var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
var cors = require('cors')
require('./database/connect')  //引入数据库连接文件
var {checkToken} = require('./utils/auth-token')
const { createProxyMiddleware } = require('http-proxy-middleware'); //引入代理模块

var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');
var stuRouter = require('./routes/stu');
var accountRouter = require('./routes/account');
var localRouter = require('./routes/local');

var app = express();
app.use(cors())  //处理服务端接口跨域问题
app.use('/hehe', createProxyMiddleware({ 
    target: 'https://shopapi.smartisan.com', 
    changeOrigin: true,
    pathRewrite:{ //提出多余路径
        '^/hehe':''
    } 
}))
// app.use('/local', createProxyMiddleware({ 
//     target: 'http://localhost:3000', 
//     changeOrigin: true,
//     pathRewrite:{ //提出多余路径
//         '^/local':''
//     } 
// }))
// app.use('/node', createProxyMiddleware({ 
//     target: 'http://localhost:3000', 
//     changeOrigin: true,
//     pathRewrite:{ //提出多余路径
//         '^/node':''
//     } 
// }))


app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use('/', indexRouter);
app.use('/node/users', usersRouter);
app.use('/local', localRouter);
app.use('/stu',stuRouter);
app.use('/node/account',(req,res,next)=>{checkToken(req,res,next)} ,accountRouter); //这个路由模块需要token验证后访问

module.exports = app;
